﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Maticsoft.DBUtility;

public partial class WebPage_SchoolInfo : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            this.GV_SCHOOLINFO.DataBind();          
        }

        ((Label)ChangePageNum1.FindControl("GV")).Text = GV_SCHOOLINFO.ID;//自定义翻页控件指向当前的表
        ((Label)ChangePageNum1.FindControl("DS")).Text = SDS_SCHOOLINFO.ID;//自定义翻页控件指向当前的数据源  
       　//ChangePageNum1.DBbind(((Label)ChangePageNum1.FindControl("lblCpi")).Text);
    }

    protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#edf1f8';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
        }
        //e.Row.Cells[1].Visible = false;//用于那些列不显示
        //e.Row.Cells[3].Visible = false;
    }

    protected void GVSch_RowCommand(object sender, GridViewCommandEventArgs e)    // 删除，详情查看事件
    {
         GridView dv = (GridView)sender;

        if (e.CommandName == "BtDetails")
        {
            int index = Convert.ToInt32(e.CommandArgument);          
            //Convert.ToInt32(dv.Rows[index].Cells[1].Text.ToString());//注意：此句可以获取gradview里面的数据
            string num = dv.DataKeys[index].Value.ToString();
            string url = "SchoolInfoDetail.aspx?State=3&table=Tab_SchoolInfo&key=" + num.ToString();
            //string title = "school";
            string temp = "<script>window.open('SchoolInfoDetail.aspx?State=3&table=Tab_SchoolInfo&key=1','','width=500, height=500,top=200,left=200,toolbar=no,menubar=no,scrollbars=yes,resizable=no,status=no,titlebar=0');</script>";
            this.Page.RegisterClientScriptBlock("Warn", temp);

            //url = "btsydhwlchart.aspx";
            //this.Page.RegisterClientScriptBlock("Warn", "<script>window.open('" + url + "');</script>");
        }
    }

    //点击选中删除
    protected void Delete_Click(object sender, EventArgs e)
    {       
        string[] delresult = Del_click(sender, e);
        switch (delresult[0])
        {
            case "":
                this.Page.RegisterStartupScript("", "<script>alert('请选择要删除的选项！');</script>");
                break;
            case "0":
                this.Page.RegisterStartupScript("", "<script>alert('删除操作出现异常，请联系管理员！');</script>");
                break;
            case "1":               
                ChangePageNum1.DBbind(((Label)ChangePageNum1.FindControl("lblCpi")).Text);
                this.Page.RegisterStartupScript("", "<script>alert('删除数据成功！');</script>");
                break;               
            case "3":
                this.Page.RegisterStartupScript("", "<script>alert('不能删除,您没有" + delresult[1] + "的删除权限！');</script>");
                break;
            default:                
                ChangePageNum1.DBbind(((Label)ChangePageNum1.FindControl("lblCpi")).Text);                
                break;

        };
    }

    private string[] Del_click(object sender, EventArgs e)
    {       
        string[] result = { "1", "" };
        string s = this.TbDelNo.Text;//index
        string Str = string.Empty;
       
        if (s.Length == 0)
        {
            result[0] = "";
            return result;
        }
        string[] dt = s.Substring(0, s.Length - 1).Split(',');
           
        try
        {         
            for (int i = 0; i < dt.Length; i++)
            {
                Str += "'" + dt[i] + "',";              
            }
            string sqlcmd = " DELETE FROM TAB_SCHOOLINFO WHERE " + GV_SCHOOLINFO.DataKeyNames[0] + " in (" + Str.Substring(0, Str.Length - 1).ToString() + ") ";
            DbHelperSQL.ExecuteSql(sqlcmd);
            result[1] = Str;
           
        }
        catch (Exception ex)
        {
            result[0] = "0";
            result[1] = ex.Message.Replace('\"', ' ').Replace('\'', ' ').Replace('\n', ' ');        
        }

        this.TbDelNo.Text = string.Empty;
        return result;
    }

    //自定义查询调用
    protected void BtnCustomSearch_Click(object sender, EventArgs e)
    {
        //现在为空
    }

    //导出
    protected void BtnExport_Click(object sender, EventArgs e)
    {
        // initGV();
        GridView GV = this.GV_SCHOOLINFO;
        if (GV.Rows.Count.Equals(0))
            return;
        GV.Columns[0].Visible = false;
        GV.Columns[GV.Columns.Count - 1].Visible = false;

        GV.Rows[0].Visible = false;
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.Charset = "gb2312";
        Response.ContentType = "application/vnd.xls ";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GV.AllowPaging = false;
        GV.DataBind();
        GV.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for
    }


    //清空查询条件
    protected void BtnClear_Click(object sender, EventArgs e)
    {
        TbSchName.Text = string.Empty;
        TbSchAddress.Text = string.Empty;

    }

    protected void DDL_DataBound(object sender, EventArgs e)
    {
        DropDownList DDL = (DropDownList)sender;
        DDL.Items.Insert(0, "全部");
    }


    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        string Strsds = string.Empty;
        if (TbSchName.Text.ToString() != "")
        {
            Strsds += " SchName like '%" + TbSchName.Text.ToString() + "%' and ";
        }

        if (TbSchAddress.Text.ToString() != "")
        {
            Strsds += " SchAddress like '%" + TbSchAddress.Text.ToString() + "%' and ";
        }

        //不为空去掉and
        if (Strsds.ToString() != "")
        {
            Strsds = Strsds.ToString().Substring(0, Strsds.Length - 4);
        }

        SDS_SCHOOLINFO.FilterExpression = Strsds;
        // GV.DataBind();
        ChangePageNum1.DBbind(((Label)ChangePageNum1.FindControl("lblCpi")).Text);
    }



    protected void CheckItemBox(object sender, EventArgs e)
    {       
        //CheckBox cb = this.Ck_Item1;
        // for (int i=0; i<cb.length; i++)
        //    cb[i].checked = e.checked;
        //  //Recod(itemName,txt);
    }

    protected void CheckAllBox(object sender, EventArgs e)
    {

        //CheckBox cb = this.GV_SCHOOLINFO.;
       
        // for (int i=0; i<cb.length; i++)
        //    cb[i].Checked = ((CheckBox)sender).Checked;
          //Recod(itemName,txt);
    }
}
